//
//  ZHScrollView.m
//  LT
//
//  Created by dllo on 16/5/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ZHScrollView.h"
//类的延展


@interface ZHScrollView ()<UIScrollViewDelegate>//签协议,以调其方法

@property (strong, nonatomic) UIPageControl *myPage;

@end

@implementation ZHScrollView

//重写初始化方法
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.page = 1;
        self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        //边框回弹取消
        self.scroll.bounces = NO;
        self.scroll.delegate = self;
        self.scroll.showsHorizontalScrollIndicator = NO;
        self.scroll.showsVerticalScrollIndicator = NO;
        //self.scroll.pagingEnabled = YES;
        [self addSubview:_scroll];
        
        //self.myPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0.4 * frame.size.height,frame.size.width, frame.size.height)];
        //[self addSubview:self.myPage];
        
        //非当前页的小点点颜色
        //self.myPage.pageIndicatorTintColor = [UIColor grayColor];
        
        //当前页的小点点颜色
        //self.myPage.currentPageIndicatorTintColor = [UIColor blackColor];
        
    }
    
    return self;
}

#pragma mark 给scrollView添加图片
-(void)setImages:(NSMutableArray *)names
{
    //
    //self.myPage.numberOfPages = names.count;
   
    NSString *first = [names firstObject];
    NSString *last = [names lastObject];
    [names insertObject:last atIndex:0];
    [names addObject:first];
    
    
    self.scroll.contentSize = CGSizeMake(self.scroll.frame.size.width *names.count, 0);
    
    //调用一个createTimer的方法
    [self createTimer];
    
    NSMutableArray *arr = [NSMutableArray arrayWithArray:names];
    int i = 0;
    //NSLog(@"count = %ld",arr.count);
    for (NSString *name in arr) {
        
        UIImageView *im = [[UIImageView alloc]initWithFrame:CGRectMake(self.scroll.frame.size.width * i , 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        im.backgroundColor = [UIColor yellowColor];
        im.tag = 10000 + i;
        NSURL *url = [NSURL URLWithString:name];
        [im sd_setImageWithURL:url];
        
        [self.scroll addSubview:im];
        _scroll.pagingEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [im addGestureRecognizer:tap];
        im.userInteractionEnabled = YES;
        
        i ++;

    }
    [self.scroll setContentOffset:CGPointMake(self.frame.size.width, 0)];
 

}


#pragma mark --创建定时器
-(void)createTimer
{

    self.zhTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
#pragma mark --定时器调用的方法
-(void)timerAction:(NSTimer *)timer
{
//当正常滚动时
    self.page ++;
    //将要到达偏移量的宽度
    CGFloat offWith = _scroll.frame.size.width * self.page;
   // NSLog(@"with = %f",offWith);
    [self.scroll setContentOffset:CGPointMake(offWith, 0) animated:YES];
   // NSLog(@"正常滚动");

    //当不是正常滚动的时候到边缘就取消动画
    NSInteger number = self.scroll.contentSize.width / self.scroll.frame.size.width;
    //number是当前图片个数
    if (offWith == self.scroll.frame.size.width * (number - 1)) {
        self.page = 1;
        [self.scroll setContentOffset:CGPointMake(0, 0)];
        //NSLog(@"快速跳转");
    }
    //还有这个
    //self.myPage.currentPage = self.page - 1;
    
}

#pragma mark-- 停止定时器
-(void)stopTimer
{
    if (self.zhTimer) {
        if (self.zhTimer.isValid) {//如果是开启状态
            [self.zhTimer invalidate];
            self.zhTimer = nil;
        }
    }


}

#pragma mark -- 手指将要拖拽的时候停止计时器
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{

    [self stopTimer];
   // NSLog(@"timer = %@",self.zhTimer);
}

#pragma mark -- 将要结束拖拽,也就是手指头离开轮播图的时候
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{

    [self createTimer];
}

#pragma mark -- 加速结束的时候
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGSize size = scrollView.contentSize;
    CGFloat width = scrollView.frame.size.width;
    self.page = scrollView.contentOffset.x / width;
    
//往左滑,如果滑到最后一张
    if (scrollView.contentOffset.x == (size.width - width)) {
        self.page = 1;
        [scrollView setContentOffset:CGPointMake(width, 0)];
    }
    //向右滑,如果滑到第一张图
    if (scrollView.contentOffset.x == 0) {
        self.page = scrollView.contentSize.width / scrollView.frame.size.width - 2;
        [scrollView setContentOffset:CGPointMake(size.width - width * 2, 0)];
    }
    //
    //self.myPage.currentPage = self.page - 1;
}

#pragma mark tapAction;
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    //block传值第二步
    self.scrollBlock(self.page - 1);
   // NSLog(@"page = %ld",self.page);
    //获得点击的视图
   // UIImageView *image = (UIImageView *)tap.view;
    //NSLog(@"image = %ld",image.tag);
    //NSLog(@"123");
    return;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
